import { useRef } from 'react';
import ReactHTMLTableToExcel from 'react-html-table-to-excel';

/**
 * 包裹ReactHTMLTableToExcel组件，使其支持span展示
 * @type 展示类型（默认为span）
 */
export const AxReactHTMLTableToExcel = props => {
  const { type = 'span', ...rest } = props;
  const ref = useRef(null);

  const handleDownload = () => {
    ref.current.handleDownload();
  };

  return type === 'span' ? (
    <>
      <span onClick={handleDownload}>{props.buttonText}</span>
      <span hidden={true}>
        <ReactHTMLTableToExcel ref={ref} {...rest} />
      </span>
    </>
  ) : (
    <ReactHTMLTableToExcel {...rest} />
  );
};
